<template>
  <div style="width: 100%;height: 100%">
    <Button icon="ios-search" type="primary" @click="todoCalculate">自动计算</Button>
    <div class="contentDiv">
      <vxe-table border
                 auto-resize
                 row-id="id"
                 :row-key="true"
                 show-overflow="tooltip"
                 highlight-hover-row
                 align="center"
                 header-align="center"
                 ref="xTree"
                 :loading="Loading"
                 :data="tableData">
        <vxe-table-column field="name" title="人员姓名" align="center"/>
        <vxe-table-column field="ssbi0003" title="分类救助上浮类别" align="center"/>
        <vxe-table-column field="ssbi0006" title="上浮比例" align="center" :formatter="renderSsbi0006"/>
        <vxe-table-column field="ssbi0005" title="分类救助金额" align="center"/>
        <vxe-table-column field="ssbi0008" title="高龄补贴金额" align="center"/>
      </vxe-table>
      <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
            @on-change="handleTableChange" :current="pageNo"></Page>
    </div>
    <div style="height: 20px;"></div>
    <Button icon="ios-search" type="primary" @click="todoIncome">自动计算</Button>
    <div class="contentDiv">
      <vxe-table border
                 auto-resize
                 row-id="id"
                 :row-key="true"
                 show-overflow="tooltip"
                 highlight-hover-row
                 align="center"
                 header-align="center"
                 ref="xTree"
                 :loading="SubtractLoading"
                 :data="subtractData">
        <vxe-table-column field="memberName" title="人员姓名" align="center"/>
        <vxe-table-column field="subtractType" title="核减类型" align="center"/>
        <vxe-table-column field="subtractTypeInfo" title="核减类型说明" align="center"/>
        <vxe-table-column field="subtractMoney" title="核减金额" align="center"/>
      </vxe-table>
    </div>
    <ss11011-modal ref="modalForm" @ok="modalFormOk"/>
    <div id="buttonDiv">
      <Button @click="prevStep" style="margin-right: 10px">上一步</Button>
      <Button type="primary" @click="nextStep" :loading="btnLoading">下一步</Button>
    </div>
  </div>
</template>

<script>
import {getAction, postAction} from '@/api/request'
import {CommonMixin} from '@/views/common/js/CommonMixin'
import '@/views/common/css/common.css';
import Ss11011Modal from './modules/Ss11011Modal'

export default {
  name: "application2",
  mixins: [CommonMixin],
  components: {Ss11011Modal},
  data() {
    return {
      url: {
        list: "/api/ac/subsistence/ss11011Service/page",
        calculate: "/api/ac/subsistence/ss11011Service/createCalculate",
        calculateIncome: '/api/ac/subsistence/ss12001Service/createCalculateIncome',
        getSubtract: '/api/ac/subsistence/ss12001Service/createSubtract',
        delete: '/api/ac/subsistence/ss12001Service/deleteSubtract'
      },
      Loading: false,
      familyInfo: this.$parent.familyInfo,

      queryParam: {
        ssbi0002: this.$parent.familyInfo.ss11001Id
      },
      btnLoading: false,
      SubtractLoading: false,
      subtractData: []
    }
  },
  methods: {
    prevStep() {
      this.$emit('prevStep');
    },
    nextStep() {
      this.$emit('nextStep');
    },

    //计算分类上浮
    async floating() {
      this.Loading = true;
      //计算分类上浮
      var result = await postAction(this.url.calculate, {ss12001Id: this.familyInfo.ss12001Id});
      if (result.data.errcode != 0) {
        this.$Message.error(result.data.errmsg);
      }
      this.loadData();
    },
    //计算收入信息
    async income() {
      this.SubtractLoading = true;
      var incomeResult = await postAction(this.url.calculateIncome, {ss12001Id: this.familyInfo.ss12001Id});
      if (incomeResult.data.errcode != 0) {
        this.$Message.error(incomeResult.data.errmsg);
      }
      this.loadSubtractData();
    },
    loadSubtractData() {
      postAction(this.url.getSubtract, {ss12001Id: this.familyInfo.ss12001Id, readOnly: false}).then((res) => {
        if (res.data.errcode != 0) {
          this.$Message.error(res.data.errmsg);
        } else {
          this.subtractData = res.data.data;
        }
      }).finally(() => {
        this.SubtractLoading = false;
      })
    },

    load() {
      this.floating();
      this.income();
    },
    todoCalculate() {
      var that = this;
      this.$Modal.confirm({
        title: '自动计算将会删除家庭现有分类救助信息，是否确认操作？',
        onOk: () => {
          that.floating();
        }
      })
    },
    todoIncome() {
      var that = this;
      this.$Modal.confirm({
        title: '自动计算将会删除家庭现有核减信息，是否确认操作？',
        onOk: () => {
          that.income();
        }
      })
    },
    handleDelete: function (id) {
      this.$Modal.confirm({
        title: '确定删除吗？',
        onOk: () => {
          if (!this.url.delete) {
            this.$Message.error("请设置url.delete属性!")
            return
          }
          var that = this;
          postAction(that.url.delete, {id: id}).then((res) => {
            if (res.data.errcode == 0) {
              that.$Message.success("删除成功！");
              this.income();
            } else {
              that.$Message.error(res.data.errmsg);
            }
          });
        }
      })
    },
    modalFormOk() {
      this.income();
    }
  },
  created() {
    this.load();
  }
}
</script>

<style scoped>
.contentDiv {
  position: relative;
  top: 10px;
  width: 100%;
}
</style>
